package cn.sweet.config;

import com.intellij.openapi.options.Configurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Objects;

/**
 * 应用配置
 *
 * @author wellhor Zhao
 * @version 1.0
 * @date 2021/11/24 4:52 下午
 **/
public class AppSettingsConfigurable implements Configurable {

    private AppSettingsComponent mySettingsComponent;

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "Little-Sweet-Plugin";
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return mySettingsComponent.getPreferredFocusedComponent();
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        mySettingsComponent = new AppSettingsComponent();
        return mySettingsComponent.getPanel();
    }

    @Override
    public boolean isModified() {
        AppSettingState settings = AppSettingState.getInstance();
        boolean modified = !mySettingsComponent.getUserNameText().equals(settings.getUserName());
        modified |= !mySettingsComponent.getTimeFormat().equals(settings.getTimeFormat());
        modified |= !mySettingsComponent.getLastTimeText().equals(settings.getLastEditTime());
        modified |= !mySettingsComponent.getLastEditPersonText().equals(settings.getLastEditPerson());
        modified |= !mySettingsComponent.getSuffix().equals(settings.getSuffix());
        modified |= !mySettingsComponent.getMaxLineCountText().equals(String.valueOf(settings.getMaxCheckLineCount()));
        modified |= (mySettingsComponent.getIsAnyChange() != (settings.getIsAnyChange()));
        return modified;
    }

    @Override
    public void apply() {
        AppSettingState settings = AppSettingState.getInstance();
        settings.setUserName(mySettingsComponent.getUserNameText());
        settings.setTimeFormat(mySettingsComponent.getTimeFormat());
        settings.setLastEditPerson(mySettingsComponent.getLastEditPersonText());
        settings.setLastEditTime(mySettingsComponent.getLastTimeText());
        settings.setSuffix(mySettingsComponent.getSuffix());
        settings.setIsAnyChange(mySettingsComponent.getIsAnyChange());

        try {
            int maxLine = Integer.parseInt(mySettingsComponent.getMaxLineCountText());
            settings.setMaxCheckLineCount(maxLine);
        } catch (NumberFormatException ignore){
            settings.setMaxCheckLineCount(15);
        }
    }

    @Override
    public void reset() {
        AppSettingState settings = AppSettingState.getInstance();
        mySettingsComponent.setUserNameText(settings.getUserName());
        mySettingsComponent.setTimeFormat(settings.getTimeFormat());
        mySettingsComponent.setLastTimeText(settings.getLastEditTime());
        mySettingsComponent.setLastEditPersonText(settings.getLastEditPerson());
        mySettingsComponent.setSuffix(settings.getSuffix());
        mySettingsComponent.setMaxLineCountText(Objects.isNull(settings.getMaxCheckLineCount()) ? "15" : String.valueOf(settings.getMaxCheckLineCount()));
        mySettingsComponent.setIsAnyChange(settings.getIsAnyChange());
    }

    @Override
    public void disposeUIResources() {
        mySettingsComponent = null;
    }

}
